package com.baihan.angus.app.game.busshow.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.baihan.angus.app.game.basic.utils.imageview.GlideCircleWithBorder;
import com.baihan.angus.app.game.busshow.R;

public class ItemMenuView extends LinearLayout {

    private String name, tip, subText;//名称 副文本
    private Drawable icon, subImage;//图标 副图标
    private boolean showRedPointTip, showToMooreIcon;//显示红点提示
    private TextView tvName, tvTip, tvSubText;//名称 副文本 View
    private ImageView ivIcon, ivSubImage, ivToMore;//图标 副图标 View
    private View redPointView;//红点 View
    private FrameLayout flIcon;

    public ItemMenuView(Context context) {
        super(context);
        init();
    }

    public ItemMenuView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        readAttrs(context, attrs);
        init();
    }

    public ItemMenuView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        readAttrs(context, attrs);
        init();
    }

    private void readAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemMenuView);
        name = typedArray.getString(R.styleable.ItemMenuView_imv_name);
        tip = typedArray.getString(R.styleable.ItemMenuView_imv_tip);
        subText = typedArray.getString(R.styleable.ItemMenuView_imv_sub_text);
        icon = typedArray.getDrawable(R.styleable.ItemMenuView_imv_icon);
        subImage = typedArray.getDrawable(R.styleable.ItemMenuView_imv_sub_image);
        showRedPointTip = typedArray.getBoolean(R.styleable.ItemMenuView_imv_red_point_tip, false);
        showToMooreIcon = typedArray.getBoolean(R.styleable.ItemMenuView_imv_show_to_more_icon, true);
        typedArray.recycle();
    }

    private void init() {
        View v = LayoutInflater.from(getContext()).inflate(R.layout.item_menu, this);
        this.setGravity(Gravity.CENTER_VERTICAL);
        tvName = v.findViewById(R.id.tvName);
        tvTip = v.findViewById(R.id.tvTip);
        tvSubText = v.findViewById(R.id.tvSubText);
        ivIcon = v.findViewById(R.id.ivIcon);
        ivToMore = v.findViewById(R.id.ivToMore);
        flIcon = v.findViewById(R.id.flIcon);
        ivSubImage = v.findViewById(R.id.ivSubImage);
        redPointView = v.findViewById(R.id.redPointView);

        setName(name);
        setTip(tip);
        setSubText(subText);
        setIconDrawable(icon);
        setSubImage(subImage);
        showMoreIcon(showToMooreIcon);
        showRedPointTip(showRedPointTip);

    }

    public void showMoreIcon(boolean showToMooreIcon) {
        this.showToMooreIcon = showToMooreIcon;
        ivToMore.setVisibility(showToMooreIcon ? View.VISIBLE : View.GONE);
    }

    public void setSubImage(Drawable subImage) {
        this.subImage = subImage;
        if (subImage == null) {
            ivSubImage.setVisibility(GONE);
        } else {
            ivSubImage.setVisibility(VISIBLE);
            ivSubImage.setImageDrawable(subImage);
        }
    }

    public void setIconDrawable(Drawable icon) {
        this.icon = icon;
        if (icon == null) {
            flIcon.setVisibility(GONE);
        } else {
            flIcon.setVisibility(VISIBLE);
            ivIcon.setImageDrawable(icon);
        }
    }

    public void setSubText(String subText) {
        this.subText = subText;
        if (TextUtils.isEmpty(subText)) {
            tvSubText.setVisibility(GONE);
        } else {
            tvSubText.setVisibility(VISIBLE);
            tvSubText.setText(subText);
        }
    }

    public void setSubTextColor(int color) {
        tvSubText.setTextColor(color);
    }

    public void setName(String name) {
        this.name = name;
        if (TextUtils.isEmpty(name)) {
            tvName.setVisibility(GONE);
        } else {
            tvName.setVisibility(VISIBLE);
            tvName.setText(name);
        }
    }

    public void setTip(String tip) {
        this.tip = tip;
        if (TextUtils.isEmpty(tip)) {
            tvTip.setVisibility(GONE);
        } else {
            tvTip.setVisibility(VISIBLE);
            tvTip.setText(tip);
        }
    }

    /**
     * 是否显示红点
     *
     * @param show
     */
    public void showRedPointTip(boolean show) {
        this.showRedPointTip = show;
        if (showRedPointTip) {
            redPointView.setVisibility(VISIBLE);
        } else {
            redPointView.setVisibility(GONE);
        }
    }

    /**
     * @return 副图标ImageView  可能是网络图片
     */
    public void setRemoteIvSubImage(String imageUrl, int errorReCId) {
        ivSubImage.setVisibility(VISIBLE);
        Glide.with(getContext()).load(imageUrl).
                apply(RequestOptions.bitmapTransform(new GlideCircleWithBorder(1, ContextCompat.getColor(getContext(), R.color.textColor3)))
                        .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC))
                .error(Glide.with(getContext()).load(errorReCId))
                .into(ivSubImage);
    }

}
